package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.modbus.utils.CacheUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.IotDeviceVo;
import com.ruoyi.system.domain.vo.IotShowDeviceVariableVo;
import com.ruoyi.system.domain.vo.IotShowTemplateVo;
import com.ruoyi.system.mapper.IotShowTemplateMapper;
import com.ruoyi.system.service.IotShowTemplateService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

@Service
public class IotShowTemplateServiceImpl implements IotShowTemplateService {

    @Autowired
    private IotShowTemplateMapper iotShowTemplateMapper;

    @Override
    public List<IotShowDevice>  getTemplate() {
        LoginUser loginUser = getLoginUser();
//        loginUser.setDeptId(208L);
        List<IotShowDevice> iotShowDeviceList = iotShowTemplateMapper.queryIotShowDeviceListByDeptId(loginUser.getDeptId());
        if(CollectionUtil.isEmpty(iotShowDeviceList)){
            return Lists.newArrayList();
        }
        for (IotShowDevice iotDeviceVo : iotShowDeviceList) {
            if (CacheUtils.get(iotDeviceVo.getSn()) != null) {
                iotDeviceVo.setDeviceStatus(0L);
            } else {
                iotDeviceVo.setDeviceStatus(1L);
            }
        }
        iotShowDeviceList.forEach(a->{
            List<IotShowDeviceVariableVo> iotShowDeviceVariableList = iotShowTemplateMapper.queryIotShowDeviceVariableListByDeviceId(a.getDeviceId());
            a.setIotShowDeviceVariableList(iotShowDeviceVariableList);
        });
        return iotShowDeviceList;
    }

    @Override
    public List<IotShowTemplate> selectIotShowTemplateList(IotShowTemplate iotShowTemplate) {
        return iotShowTemplateMapper.selectIotShowTemplateList(iotShowTemplate);
    }

    @Override
    public int deleteIotShowTemplateByIds(Long[] ids)
    {
        return iotShowTemplateMapper.deleteIotShowTemplateByIds(ids);
    }

    @Override
    @Transactional
    public void saveShowTemplate(IotShowTemplateUpload dataModel) {
        IotShowTemplate iotShowTemplate = new IotShowTemplate();
        LoginUser loginUser = getLoginUser();
        iotShowTemplate.setTemplateName(dataModel.getTemplateName());
        iotShowTemplate.setDeptId(loginUser.getDeptId());
        iotShowTemplateMapper.insertIotShowTemplate(iotShowTemplate);

        dataModel.getDevices().forEach(a->{
            IotShowDevice iotShowDevice = new IotShowDevice();
            iotShowDevice.setShowTemplateId(iotShowTemplate.getId());
            iotShowDevice.setDeviceId(a.getDeviceId());
            iotShowDevice.setPosition(a.getPosition());
            iotShowDevice.setTitle(a.getTitle());
            iotShowTemplateMapper.insertIotShowDevice(iotShowDevice);

            a.getFields().forEach(b->{
                IotShowDeviceVariable iotShowDeviceVariable = new IotShowDeviceVariable();
                iotShowDeviceVariable.setShowDeviceId(iotShowDevice.getId());
                iotShowDeviceVariable.setLable(b.getLabel());
                iotShowDeviceVariable.setKey(b.getKey());
                iotShowDeviceVariable.setShow(b.getShow());
                iotShowDeviceVariable.setVariableId(b.getVariableId());
                iotShowTemplateMapper.insertIotShowDeviceVariable(iotShowDeviceVariable);
            });
        });
    }

}
